<template>
  <el-form :model="form" label-width="120px">
    <el-form-item label="客户姓名">
      <el-input :disabled="props.type!='edit'" v-model="form.clientName" style="max-width: 200px" />
    </el-form-item>
    <el-form-item label="客户电话">
      <el-input :disabled="props.type!='edit'" v-model="form.telephone" style="max-width: 200px" />
    </el-form-item>
    <el-form-item label="客户所在地">
      <el-select
        :disabled="props.type!='edit'"
        v-model="form.location"
        placeholder="请选择"
        style="max-width: 200px"
      >
        <el-option label="中国" value="shanghai" />
        <el-option label="外国" value="beijing" />
      </el-select>
    </el-form-item>
    <el-form-item label="是否VIP">
      <el-switch
        :disabled="props.type!='edit'"
        v-model="form.isVip"
        active-value="1"
        inactive-value="0"
      />
    </el-form-item>
    <el-form-item label="业务类型">
      <el-radio-group :disabled="props.type!='edit'" v-model="form.businessType">
        <el-radio
          v-for="item in businessTypeDicItems"
          :label="item.code"
          :key="item.code"
        >{{item.value}}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="详情">
      <el-input :disabled="props.type!='edit'" v-model="form.detail" type="textarea" />
    </el-form-item>
    <el-form-item v-show="props.type=='edit'">
      <el-button type="primary" @click="save()">保存</el-button>
    </el-form-item>
  </el-form>
</template>
  
  <script>

import request from '@/components/bus/request';

export default {
  name: "ClientForm",
  props: ["chatId", "show", "type", "clientId"],
  data() {
    return {
      businessTypeDicItems: [],
      form: {
        clientName: "",
        telephone: "",
        location: "",
        businessType: "",
        isVip: "1",
        detail: ""
      },
      ticketId: null
    };
  },
  mounted() {
    this.loadBusinessTypeItems();
    if (this.type === "edit") {
      this.loadClientInfo();
    }
    if (this.show) {
      this.loadTicket();
    }
  },
  watch: {
    show(newValue) {
      if (newValue) {
        this.loadTicket();
      }
    }
  },
  methods: {
    loadBusinessTypeItems() {
      request.get("/api/useDic/getDicItems/businessType").then(resp => {
        this.businessTypeDicItems.push(...resp);
      });
    },
    loadClientInfo() {
      request.get(`/api/chat/getClientInfo/${this.clientId}`).then(resp => {
        this.form.clientName = resp.customerName;
        this.form.telephone = resp.telephone;
      });
    },
    loadTicket() {
      request.get(`/api/chat/getTicket/${this.chatId}`).then(resp => {
        for (let key in resp) {
          this.form[key] = resp[key];
        }
      });
    },
    save() {
      this.form.chatId = this.chatId;
      if (this.ticketId === null) {
        this.addTicket();
      } else {
        this.updateTicket();
      }
    },
    addTicket() {
      request.post("/api/online/addTicket", this.form).then(resp => {
        this.ticketId = resp.id;
        this.form.id = this.ticketId;
        this.$message({
          message: "添加成功！",
          type: "success"
        });
      });
    },
    updateTicket() {
      request.post("/api/online/updateTicket", this.form).then(() => {
        this.$message({
          message: "修改成功！",
          type: "success"
        });
      });
    }
  }
};
</script>